package services

import (
    "context"
    "gitee.com/go-libs/db-xorm/db"
    "integration/app/models"
)

func (o *ErpCustomerService) Edit(ctx context.Context, req *models.ErpCustomer) (affects int64, err error) {
    var sess *db.Session

    if sess, err = o.Master(ctx); err != nil {
        return
    }

    affects, err = sess.Cols(
        "erp_id",
        "erp_frame",
        "erp_number",
        "erp_name",
        "erp_contact_name",
        "erp_contact_phone",
        "erp_contact_address",
    ).Where("`id` = ?", req.Id).Update(req)
    return
}

func (o *ErpCustomerService) GetByErpNumber(ctx context.Context, number string) (model *models.ErpCustomer, err error) {
    var (
        has  bool
        sess *db.Session
    )

    if sess, err = o.Slave(ctx); err != nil {
        return
    }

    model = &models.ErpCustomer{}

    if has, err = sess.Where("`erp_number` = ?", number).Get(model); err != nil || !has {
        model = nil
    }
    return
}

func (o *ErpCustomerService) SetErpIdByErpNumber(ctx context.Context, id int, number string) (affects int64, err error) {
    var (
        bean *models.ErpCustomer
        sess *db.Session
    )

    if sess, err = o.Master(ctx); err != nil {
        return
    }

    bean = &models.ErpCustomer{ErpId: id}
    affects, err = sess.Cols("erp_id").Where("`erp_number` = ?", number).Update(bean)
    return
}
